/*
展示 finalize执行中, 对象的自我拯救

output:
======1======
execute finalize
i'm alive
======2=====
dead

*/
public class FinalizeEscapeGC {
	private static FinalizeEscapeGC SAVE_HOOK = null;

	public void isAlive() {
		System.out.println("i'm alive");
	}

	@Override
	public void finalize() throws Throwable {
		super.finalize();
		System.out.println("execute finalize");
		FinalizeEscapeGC.SAVE_HOOK = this;
	}

	public static void main(String[] args) throws Throwable{
		SAVE_HOOK = new FinalizeEscapeGC();

		System.out.println("======1======");
		SAVE_HOOK = null;
		System.gc();
		Thread.sleep(500);
		if(SAVE_HOOK == null) System.out.println("dead");
		else SAVE_HOOK.isAlive();

		System.out.println("======2=====");
		SAVE_HOOK = null;
		System.gc();
		Thread.sleep(500);
		if(SAVE_HOOK == null) System.out.println("dead");
		else SAVE_HOOK.isAlive();
	}
}